package com.sys.plat.web.core;

import com.sys.plat.util.SpringUtil;
import com.sys.plat.util.StrutsUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: mabo
 * Date: 2009-2-24
 * Time: 22:19:20
 * To change this template use File | Settings | File Templates.
 */
public class EncodingFilter implements Filter{

    public static boolean isqa = false;

    public void destroy() {
        // TODO Auto-generated method stub
    }

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        chain.doFilter(request, response);
        if (isqa) {
            ServletContext context = ((HttpServletRequest) request).getSession().getServletContext();
            StrutsUtil.reloadConfig(context);
            SpringUtil.reLoadSpringConfig(context);
        }
    }

    public void init(FilterConfig conf) throws ServletException {
        String qa = conf.getServletContext().getInitParameter("qa");
        if (null != qa) {
            if ("true".equals(qa)) {
                isqa = true;
            }
        }
    }
}
